﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        var renderTable;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form', 'dropdown'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , dropdown = layui.dropdown;

            renderTable = function () {
                table.render({
                    elem: '#table-datalist'
                    , url: '/Console/RebateScheme/GetListJson'
                    , title: '数据表'
                    , cols: [[
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'names', title: '名称' }
                        , {
                            field: 'state', title: '状态', templet: function (res) {
                                if (res.state == 1)
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '" checked="checked">'
                                else
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '">'
                            }
                        }
                        , { field: 'sort', title: '排序' }
                        , { field: 'creator', title: '创建人' }
                        , { field: 'createDate', title: '创建时间', width: 180 }
                        , { field: 'remark', title: '备注' }
                        , { fixed: 'right', title: '操作', toolbar: '#table-datalist-bar' }
                    ]]
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');
                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                    }
                });
            };
            //加载表格
            renderTable();

            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    top.OpenPageByKey(layui, "edit", 2, '600px', '500px', '/Console/RebateScheme/Edit?id=' + data.id);
                } else if (obj.event === 'more') {
                    //下拉菜单
                    dropdown.render({
                        elem: this //触发事件的 DOM 对象
                        , show: true //外部事件触发即显示
                        , data: [{
                            title: top.lang.rebateLevel
                            , id: 'rebateLevel'
                        }, {
                            title: top.lang.del
                            , id: 'del'
                        }]
                        , click: function (menudata) {
                            if (menudata.id === 'rebateLevel') {
                                parent.layui.index.openTabsPage("/Console/RebateLevel/Index?schemeId=" + data.id, data.names);
                                parent.layui.element.render('nav');
                            } else if (menudata.id === 'del') {
                                var postData = {
                                    id: data.id
                                };
                                layui.layer.confirm(top.lang.confirmDelete, {
                                    title: top.lang.confirmTitle,
                                    btn: [top.lang.confirmBtn, top.lang.closeBtn]
                                }, function (index, layero) {
                                    $.ajax({
                                        type: "POST",
                                        url: "/Console/RebateScheme/Delete",
                                        data: postData, // 你的formid
                                        async: true,
                                        error: function (request) {
                                            alert("Connection error");
                                        },
                                        beforeSend: function (xhr) {
                                            layui.layer.load();
                                        },
                                        success: function (data) {
                                            var item = eval(data);
                                            if (item.code === 0) {
                                                top.ShowMessageByKey(layui, "deleteSuccess", 6, 1000, function () {
                                                    renderTable();
                                                });
                                            } else {
                                                top.AlertByKey(layui, item.msg, 0);
                                            }
                                            layer.closeAll('loading');
                                        }
                                    });
                                });
                            }
                        }
                        , align: 'right' //右对齐弹出（v2.6.8 新增）
                        , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                    })
                }
            });
            form.on('submit(addNew)', function (data) {
                top.OpenPageByKey(layui, "addNew", 2, '600px', '500px', '/Console/RebateScheme/Create');
            });

            //监听锁定操作
            form.on('switch(table-state)', function (obj) {
                var state = 1;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/RebateScheme/UpdateState",
                    data: {
                        id: this.value
                        , intState: state
                    }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
            });
        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-table-tool layui-form" style="box-sizing: border-box;">
                        <div class="layui-table-tool-temp">
                            <button type="button" class="layui-btn layui-btn-sm" id="addNew" lay-submit lay-filter="addNew" lay-event="addNew">新增</button>
                        </div>
                    </div>
                    <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                    <script type="text/html" id="table-datalist-bar">
                        <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">编辑</button>
                        <button class="layui-btn layui-btn-sm" lay-event="more">更多</button>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>